package com.skivingcloud.develop.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.skivingcloud.admin.utils.CommonConstant;
import com.skivingcloud.common.utils.PageParameter;
import com.skivingcloud.common.utils.UuidUtil;
import com.skivingcloud.develop.entity.SysFormDesigner;
import com.skivingcloud.develop.mapper.SysFormDesignerMapper;
import com.skivingcloud.develop.service.SysFormDesignerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 表单设计表 服务层实现。
 *
 * @author skivingcloud
 * @since 2024-01-18
 */
@Service
public class SysFormDesignerServiceImpl extends ServiceImpl<SysFormDesignerMapper, SysFormDesigner> implements SysFormDesignerService {

    /**
     * 分页查询表单设计表
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysFormDesigner> queryByPage(PageParameter<SysFormDesigner> parameter){
        Page<SysFormDesigner> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create().from(SysFormDesigner.class).as("oo")
                .join(
                    QueryWrapper.create().select(
                        QueryMethods.column(SysFormDesigner::getFormCode),
                        QueryMethods.max(QueryMethods.column(SysFormDesigner::getVersion)).as("version"))
                        .from(SysFormDesigner.class)
                        .groupBy(SysFormDesigner::getFormCode)).as("ii")
                .on(
                    wrapper -> wrapper.where("oo.form_code = ii.form_code")
                            .and("oo.version = ii.version")
                );
        SysFormDesigner sysFormDesigner = parameter.getParam();
        if(sysFormDesigner != null){
            if(StringUtils.isNotBlank(sysFormDesigner.getFormCode())){
                queryWrapper.and(SysFormDesigner::getFormCode).like(sysFormDesigner.getFormCode());
            }
            if(StringUtils.isNotBlank(sysFormDesigner.getFormName())){
                queryWrapper.and(SysFormDesigner::getFormName).like(sysFormDesigner.getFormName());
            }
            if(StringUtils.isNotBlank(sysFormDesigner.getRemark())){
                queryWrapper.and(SysFormDesigner::getRemark).like(sysFormDesigner.getRemark());
            }
        }
        queryWrapper.orderBy(SysFormDesigner::getFormCode);
        return this.page(page, queryWrapper);
    }
    
    /**
     * 根据名称查询表单,只查询已发布的最新版本
     *
     * @param formName 名称
     * @return 结果
     */
    @Override
    public List<SysFormDesigner> getFormDesignerByNameLike(String formName){
        QueryWrapper queryWrapper = QueryWrapper.create().from(SysFormDesigner.class).as("oo")
                .join(
                        QueryWrapper.create().select(
                                        QueryMethods.column(SysFormDesigner::getFormCode),
                                        QueryMethods.max(QueryMethods.column(SysFormDesigner::getVersion)).as("version"))
                                .from(SysFormDesigner.class)
                                .where(SysFormDesigner::getResStatus).eq(CommonConstant.COMMON_YES_OR_NO_YES)
                                .groupBy(SysFormDesigner::getFormCode)).as("ii")
                .on(
                        wrapper -> wrapper.where("oo.form_code = ii.form_code")
                                .and("oo.version = ii.version")
                );
        if (StringUtils.isNotBlank(formName)) {
            queryWrapper.where(SysFormDesigner::getFormName).like(formName);
        }
        return this.list(queryWrapper);
    }
    
    /**
     * 若源记录已发布则生成新记录，否则直接更改源记录
     *
     * @param sysFormDesigner 组件信息
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateVersion(SysFormDesigner sysFormDesigner) {
        SysFormDesigner old = this.getById(sysFormDesigner.getId());
        if (CommonConstant.COMMON_YES_OR_NO_YES.equals(old.getResStatus())) {
            //如果源记录已发布则生成新纪录且版本+1
            sysFormDesigner.setId(UuidUtil.get32UUID());
            sysFormDesigner.setVersion(sysFormDesigner.getVersion() + 1);
            return this.save(sysFormDesigner);
        }else{
            return this.updateById(sysFormDesigner);
        }
    }

    /**
     * 发布组件
     *
     * @param sysFormDesigner 组件信息
     * @return 是否成果
     */
    @Override
    public Boolean release(SysFormDesigner sysFormDesigner) {
        sysFormDesigner.setResStatus(CommonConstant.COMMON_YES_OR_NO_YES);
        return this.updateById(sysFormDesigner);
    }

    /**
     * 根据编码获取最新版本已发布的组件信息
     *
     * @param code 组件设计编码
     * @return 组件设计详情
     */
    @Override
    public SysFormDesigner getLatestInfoByCode(String code) {
        QueryWrapper queryWrapper = QueryWrapper.create().from(SysFormDesigner.class).as("oo")
                .join(
                        QueryWrapper.create().select(
                                        QueryMethods.column(SysFormDesigner::getFormCode),
                                        QueryMethods.max(QueryMethods.column(SysFormDesigner::getVersion)).as("version"))
                                .from(SysFormDesigner.class)
                                .where(SysFormDesigner::getResStatus).eq(CommonConstant.COMMON_YES_OR_NO_YES)
                                .groupBy(SysFormDesigner::getFormCode)).as("ii")
                .on(
                        wrapper -> wrapper.where("oo.form_code = ii.form_code")
                                .and("oo.version = ii.version")
                ).and(SysFormDesigner::getFormCode).eq(code);
        return this.mapper.selectOneByQuery(queryWrapper);
    }
}
